﻿function OpenClass2(e) {

    layer.open({
        id: 1,
        type: 1,
        title: '选择班级',
        area: ['40%', '30%'],
        content: ' <div class="row" style="width: 98%;  margin-left:10px; margin-top:10px;" id="Content2">'
            + '<div></div>'
            + '</div>',
        btn: ["加入", "关闭"],
        btn1: function (index, layero) {
            //班级名称
            const classText = $("[class='col-sm-2 layui-btn layui-btn-normal']").text();
            if (classText !== "") {
                var userId = $("#UserId").text().trim(); //用户id
                $.ajax({
                    type: "POST",
                    url: "/Curricula/JoinCurricula",
                    data: {
                        curriculumId: e,
                        studentId: userId,
                        className: classText
                    },
                    dataType: 'json',
                    success: function (data) {
                        if (data.tf) {
                            layer.msg("加入成功！");
                            layer.close(index);
                        } else {
                            layer.msg(data.msg);
                        }
                    }, error: function (data) {
                        layer.msg("连接服务器失败");
                    }
                });

            } else {
                layer.msg("请选择班级！");
            }
        },
        btn2: function (index, layero) {
            layer.close(index);
        }
    });
    FindClass2(e);
}

function FindClass2(e) {
    //e为课程id
    var htmlText = "";
    var indexFirst = layer.load(1, {
        time: 10 * 1000,
        shade: false //0.1透明度的白色背景
    });
    $.ajax({
        type: "POST",
        url: "/Curricula/FindClasses",
        data: {
            curriculumId: e
        },
        dataType: 'json',
        success: function (data) {
            $('#Content2').empty(); //初始化内容
            if (data.msg !== false) {
                var a = eval('(' + data + ')'); //转json对象
                //layer.msg(a[0]);
                for (let i in a) {
                    if (Object.prototype.hasOwnProperty.call(a, i)) {
                        htmlText += `<button name='classOp' class='col-sm-2 layui-btn layui-btn-primary' style='margin:8px ' onclick='onclickBtn2(${i
                            })' id='btn${i}' >${a[i].ClassName}</button>`;
                    }
                }
                $('#Content2').append(htmlText);
            } else {
                htmlText += `<p style="text-align: center;color:lightgray">此课程还没有班级呢</p>`;
                $('#Content2').append(htmlText);
            }
            layer.close(indexFirst);

        }, error: function (data) {
            layer.msg(data.msg);
            layer.close(indexFirst);
        }
    });
}
function FindCurriculum2() {
    var htmlText = "";
    $("#boxChangesImg").hide();
    const textSearch = $('#findCode').val().trim();
    $('#boxChanges').empty();
    //加载反馈动画
    const loadingGif = `<p style="text-align: center">
                <i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" style="font-size: 50px; color: lightgray">&#xe63e;</i>
            </p>`;
    $('#boxChanges').append(loadingGif);
    $.ajax({
        type: "POST",
        url: "/Curricula/ShowCurriculaByCode",
        async: false,
        data: {
            curriculaCode: textSearch
        },
        dataType: 'json',
        success: function (data) {
            //初始化内容
            if (data.msg !== false) {

                var a = eval('(' + data + ')'); //转json对象
                for (let i in a) {
                    if (Object.prototype.hasOwnProperty.call(a, i)) {
                        htmlText += `<div class="row">
                                                <div id="f_ ${a[i].Id}" class="col-md-3" style="border: solid 1px lightgray; border-radius: 5px; margin: 8px; padding-bottom: 10px; width: 20%; position: relative;background: white">
                                                    <h3 id="curriName_${a[i].Id}">${a[i].CurriName}</h3>
                                                    <span>课程码：${a[i].CurriCode}</span>
                                                    <p>
                                                        <span id="year_${a[i].Id}"> ${a[i].Years}</span>
                                                        <br />
                                                        <span >第${a[i].Semester}学期</span>
                                                    </p>
                                                    <hr />
                                                    <span>成员：${a[i].Menbers} 人</span>
                                                    <div style="position: absolute; top: 80%; left: 70%">
                                                        <button title="进入" type="button" class="layui-btn   layui-btn-normal layui-btn-sm " onclick="OpenClass2(${a[i].Id})">加入</button>
                                                        </div>
                                                    </div>
                                            </div>`;
                    }
                }
                setTimeout(function () {
                    $('#boxChanges').empty();
                    $('#boxChanges').append(htmlText);
                }, 1000);
            } else {
                setTimeout(function () {
                    $("#boxChangesImg").show();
                    $('#boxChanges').empty();
                }, 2000);


            }

        }, error: function (data) {
            layer.msg(data.msg);
        }
    });

}
function onclickBtn2(e) {
    $("button[name*='classOp']").attr("class", "col-sm-2 layui-btn layui-btn-primary");
    $(`#btn${e}`).attr("class", "col-sm-2 layui-btn layui-btn-normal");
}